package com.wang.gallerybackend.controller;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wang.gallerybackend.annotation.AuthCheck;
import com.wang.gallerybackend.common.AjaxResult;
import com.wang.gallerybackend.common.BaseResponse;
import com.wang.gallerybackend.common.DeleteRequest;
import com.wang.gallerybackend.constant.UserConstant;
import com.wang.gallerybackend.exception.BusinessException;
import com.wang.gallerybackend.exception.ErrorCode;
import com.wang.gallerybackend.exception.ThrowUtils;
import com.wang.gallerybackend.model.dto.user.*;
import com.wang.gallerybackend.model.entity.User;

import com.wang.gallerybackend.model.vo.LoginUserVO;
import com.wang.gallerybackend.model.vo.UserVO;
import com.wang.gallerybackend.service.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * 用户接口
 * @author wang
 */
@Api(tags = "用户接口")
@RestController
@RequestMapping("/user")
public class UserController {
	@Resource
	private UserService userService;
	final String DEFAULT_USER_PASSWORD = "12345678";

	@ApiOperation("用户注册")
	@PostMapping("/register")
	public BaseResponse<Long> userRegister(@RequestBody UserRegisterRequest userRegisterRequest) {
		ThrowUtils.throwIf(userRegisterRequest == null, ErrorCode.PARAMS_ERROR);
		return AjaxResult.success(userService.userRegister(userRegisterRequest));
	}

	@ApiOperation("用户登录")
	@PostMapping("/login")
	public BaseResponse<LoginUserVO> userLogin(@RequestBody UserLoginRequest userLoginRequest, HttpServletRequest request) {
		ThrowUtils.throwIf(userLoginRequest == null, ErrorCode.PARAMS_ERROR);
		return AjaxResult.success(userService.userLogin(userLoginRequest.getUserAccount(), userLoginRequest.getUserPassword(), request));
	}

	@ApiOperation("获取当前登录用户")
	@GetMapping("/get/login")
	public BaseResponse<LoginUserVO> getLoginUser(HttpServletRequest request) {
		User loginUser = userService.getLoginUser(request);
		return AjaxResult.success(userService.getLoginUserVO(loginUser));
	}

	@ApiOperation("用户注销")
	@PostMapping("/logout")
	public BaseResponse<Boolean> userLogout(HttpServletRequest request) {
		return AjaxResult.success(userService.userLogout(request));
	}

	@ApiOperation("创建用户,尽管理员可用")
	@AuthCheck(mustRole = UserConstant.ADMIN_ROLE)
	@PostMapping("/add")
	public BaseResponse<Long> add(@RequestBody UserAddRequest userAddRequest) {
		ThrowUtils.throwIf(userAddRequest == null, ErrorCode.PARAMS_ERROR);
		User user = new User();
		BeanUtil.copyProperties(userAddRequest, user);
		String encryptPassword = userService.getEncryptPassword(DEFAULT_USER_PASSWORD);
		//设置默认密码
		user.setUserPassword(encryptPassword);
		//插入数据库
		boolean save = userService.save(user);
		ThrowUtils.throwIf(!save, ErrorCode.OPERATION_ERROR);
		return AjaxResult.success(user.getId());
	}

	@ApiOperation("根据id查询用户,尽管理员可用")
	@AuthCheck(mustRole = UserConstant.ADMIN_ROLE)
	@GetMapping("/get")
	public BaseResponse<User> getUserById(Long id) {
		ThrowUtils.throwIf(id == null, ErrorCode.PARAMS_ERROR);
		User user = userService.getById(id);
		ThrowUtils.throwIf(user == null, ErrorCode.NOT_FOUND_ERROR);
		return AjaxResult.success(user);
	}

	@ApiOperation("根据id获取包装类")
	@GetMapping("/get/vo")
	public BaseResponse<UserVO> getUserVOById(@RequestParam Long id) {
		BaseResponse<User> response = getUserById(id);
		User user = response.getData();
		return AjaxResult.success(userService.getUserVO(user));
	}

	@ApiOperation("删除用户")
	@AuthCheck(mustRole = UserConstant.ADMIN_ROLE)
	@PostMapping("/delete")
	public BaseResponse<Boolean> deleteUser(@RequestBody DeleteRequest deleteRequest) {
		if (deleteRequest == null || deleteRequest.getId() <= 0) {
			throw new BusinessException(ErrorCode.PARAMS_ERROR);
		}
		boolean b = userService.removeById(deleteRequest.getId());
		return AjaxResult.success(b);
	}

	@ApiOperation("更新用户")
	@PostMapping("/update")
	public BaseResponse<Boolean> updateUser(@RequestBody UserUpdateRequest userUpdateRequest) {
		ThrowUtils.throwIf(userUpdateRequest == null
				|| userUpdateRequest.getId() == null, ErrorCode.PARAMS_ERROR);
		User user = new User();
		BeanUtil.copyProperties(userUpdateRequest, user);
		ThrowUtils.throwIf(!userService.updateById(user), ErrorCode.OPERATION_ERROR);
		return AjaxResult.success(true);
	}
	@ApiOperation("分页查询")
	@AuthCheck(mustRole = UserConstant.ADMIN_ROLE)
	@PostMapping("/list/page")
	public BaseResponse<Page<UserVO>> listUserByPage(@RequestBody UserQueryRequest userQueryRequest) {
		ThrowUtils.throwIf(userQueryRequest == null, ErrorCode.PARAMS_ERROR);
		long pageNum = userQueryRequest.getPageNum();
		long pageSize = userQueryRequest.getPageSize();
		Page<User> userPage = userService.page(new Page<>(pageNum, pageSize), userService.getQueryWrapper(userQueryRequest));
		Page<UserVO> userVOPage = new Page<>(pageNum,pageSize, userPage.getTotal());
		List<UserVO> userVO = userService.getUserVO(userPage.getRecords());
		userVOPage.setRecords(userVO);
		return AjaxResult.success(userVOPage);
	}
}
